import utime as time
from driver import UART

uart0=UART()
uart0.open("TTL")
uart1=UART()
uart1.open("RS232")
uart2=UART()
uart2.open("RS485")

test_TTL = False
test_232 = False


def main():
    global test_TTL,test_232
    #485测试
    writeBuf2=bytearray('** please  input  test **\r\n')
    uart2.write(writeBuf2)
    readBuf2=bytearray(15)
    while True:
        uart2.read(readBuf2) 
        time.sleep_ms(500)
        # print(readBuf2[:8])
        # print(readBuf2[-1:])
        if 'test' in readBuf2 :
            print('#485_test---')
            writeBuf2=bytearray('#  successed  485  test #\r\n')
            uart2.write(writeBuf2)
            break
        else:
            pass
    #TTL测试
    print('#TTL_test---')
    #time.sleep_ms(200)
    readBuf= bytearray(4)
    writeBuf=bytearray(b'\x05\x04\x03\x02')
    uart0.write(writeBuf)
    uart0.read(readBuf)
    print('writeBuf',writeBuf)
    print('readBuf',readBuf)
    if writeBuf == readBuf:
        test_TTL = True
        writeBuf2=bytearray('#  successed  TTL  test #\r\n')
        uart2.write(writeBuf2)
    else:
        writeBuf2=bytearray('!!!! fall  TTL  test !!!!\r\n')
        uart2.write(writeBuf2)
    #232测试
    print('#232_test---')
    #time.sleep_ms(200)
    readBuf1= bytearray(4)
    writeBuf1=bytearray(b'\x05\x04\x03\x02')
    uart1.write(writeBuf1)
    uart1.read(readBuf1)
    print('writeBuf1',writeBuf1)
    print('readBuf1',readBuf1)
    if writeBuf1 == readBuf1:
        test_232 = True
        writeBuf2=bytearray('#  successed  232  test #\r\n')
        uart2.write(writeBuf2)
    else:
        writeBuf2=bytearray('!!!! fall  232  test !!!!\r\n')
        uart2.write(writeBuf2)
  


if __name__ == '__main__':
    main()

